﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.DBC;
using Styx.WoWInternals.WoWObjects;
using Styx.CommonBot.Inventory;

namespace WuzzMonk
{
    public partial class Monk : CombatRoutine
    {
        private string Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

        #region Routine Information
        public sealed override string Name { get { return "WuzzMonk " + Version; } }
        public override WoWClass Class { get { return WoWClass.Monk; } }
        public override bool WantButton { get { return true; } }
        public override void OnButtonPress()
        {
            GUI.SettingsForm SettingsForm = new GUI.SettingsForm();
            SettingsForm.ShowDialog();
        }
        #endregion

        private static LocalPlayer Me { get { return StyxWoW.Me; } }

        public override void Initialize()
        {
            Logging.Write(Colors.Red, "== Thank you for using WuzzMonk " + Version + "!");
        }

        #region PreCombat Buffs
        public override bool NeedPreCombatBuffs { get { return ( !Me.Mounted && (NeedLegacyOfTheEmperor() || NeedLegacyOfTheWhiteTiger()) ); } }
        public override void PreCombatBuff()
        {
            if (!Me.Mounted)
            {
                CastLegacyOfTheEmperor();
                CastLegacyOfTheWhiteTiger();
            }
        }
        #endregion

        #region Rest Behaviour
        public override bool NeedRest { get { return (Me.HealthPercent <= 65 || (Me.Auras.ContainsKey("Food") && Me.HealthPercent < 100)); } }
        public override void Rest()
        {
            // Try to eat if we aren't already
            if (!Me.Auras.ContainsKey("Food"))
            {
                WoWItem BestFood = Consumable.GetBestFood(false);
                if (BestFood.Use())
                {
                    Logging.Write("== Eating " + BestFood.Name);
                }
            }
        }
        #endregion

        #region Pull
        public override void Pull()
        {
            // Only do this if movement is enabled
            if (BotSettings.BotMovement)
            {
                if (Me.GotTarget && !Helpers.Unit.ValidUnit(Me.CurrentTarget)) { Me.ClearTarget(); }

                // Avoid crappy combat
                if ((Me.CurrentTarget.IsFlying || Me.CurrentTarget.CurrentHealth <= 5) && Me.CurrentTarget.IsNeutral)
                {
                    Blacklist.Add(Me.CurrentTarget.Guid, TimeSpan.FromMinutes(5));
                    Me.ClearTarget();
                    Logging.Write(Colors.Red, "== Blacklisted crappy neutral mob!");
                }

                if (Me.GotTarget && Helpers.Unit.ValidUnit(Me.CurrentTarget))
                {
                    ApproachAndFaceTarget();
                    AutoAttack();

                    if (BotSettings.BotProvoke && Me.CurrentTarget.Distance < 40)
                    {
                        CastSpecial("Provoke", Me.CurrentTarget);
                    }

                    if (BotSettings.BotRoll && Me.CurrentTarget.Distance > 20)
                    {
                        CastSpecial("Roll", null);
                    }
                }
            }
        }
        #endregion

        #region Combat
        public override void Combat()
        {
            // Blacklist non-valid units
            if (!Helpers.Unit.ValidUnit(Me.CurrentTarget))
            {
                Logging.Write(Colors.Pink, "== Blacklisting {0} for 5 minutes ==", Me.CurrentTarget.Name);
                Blacklist.Add(Me.CurrentTarget.Guid, TimeSpan.FromMinutes(5));
                Me.ClearTarget();
            }

            // Check if valid target
            if( Me.GotTarget && Helpers.Unit.ValidUnit(Me.CurrentTarget)) {
                
                AutoAttack();

                // Only move if enabled
                if (BotSettings.BotMovement)
                {
                    ApproachAndFaceTarget();

                    // Roll to target
                    if (BotSettings.BotRoll && Me.CurrentTarget.Distance > 20) { CastSpecial("Roll", null); }
                }

                // Stop here if we can't cast a spell
                if (SpellManager.GlobalCooldown || Me.IsCasting || !Helpers.Unit.IsCrowdControlled(Me)) { return; }

                CastHeal();
                //UseHealthPotion();
                CastDefensiveSpells();
                CastInterrupt();

                // Select proper rotation
                if (Me.Level < 10)
                {
                    LowbieRotation();
                }
                else
                {
                    switch (Me.Specialization)
                    {
                        case WoWSpec.MonkBrewmaster:
                            BrewmasterRotation(Me.Level);
                            break;
                        case WoWSpec.MonkMistweaver:
                            MistweaverRotation(Me.Level);
                            break;
                        case WoWSpec.MonkWindwalker:
                            WindwalkerRotation();
                            break;
                        default:
                            LowbieRotation(); // Fallback until spec is chosen
                            break;
                    }
                }
            }
        }
        #endregion
    }
}